//
//  LastestVideoController.swift
//  YiYaoHui
//
//  Created by 高永效 on 15/12/6.
//  Copyright © 2015年 TMPercy. All rights reserved.
//

import UIKit
import Alamofire
import MBProgressHUD

class LastestVideoController: UIViewController {

    @IBOutlet weak var videoTableView: UITableView!
    
    var videos = [Video]()
    let latestVideoURL = "http://112.74.131.194:8080/MedicineProject/App/res/getRecentVideo" 
    let enterpriseVideoURL = "http://112.74.131.194:8080/MedicineProject/App/user/getVideoes"
    
    //接收传给播放视频页面的值
    var url: String!
    var videoTitle: String!
    //跳转用的id 
    var id : Int! //id=1（最新会议）    id=2（企业会议）
    //请求具体企业视频的id
    var enterpriseID: String!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if id == 1 {
            title = "医药视频"
            getLatestVideos()
            
        } else if id == 2 {
            title = "会议视频"
            getEnterpriseVideos()
        }

        
        videoTableView.registerNib(UINib(nibName: "VideoCell", bundle: nil), forCellReuseIdentifier: "videoCell")
        videoTableView.tableFooterView = UIView(frame: CGRectZero)
        MBProgressHUD.showHUDAddedTo(self.view, animated: true).labelText = "正在加载"
        
            }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        tabBarController?.tabBar.hidden = true
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.setNavigationBarHidden(true, animated: true)
    }
    
    func getLatestVideos() {
        Alamofire.request(.POST, latestVideoURL, parameters: ["size" : 20], encoding: ParameterEncoding.URL, headers: nil).responseJSON { (_, _, result) -> Void in
            if let value = result.value {
                let videoResults = value["Videos"] as! [Dictionary<String, AnyObject>]
                for item in videoResults {
                    let video = Video(video: item)
                    self.videos.append(video)
                }
                NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
                    self.videoTableView.reloadData()
                    MBProgressHUD.hideHUDForView(self.view, animated: true)
                })
            }
            else {
                //MARK: something error
            }
        }
    }
    
    //获取具体企业的视频列表
    func getEnterpriseVideos() {
        Alamofire.request(.POST, enterpriseVideoURL, parameters: ["id" : enterpriseID], encoding: ParameterEncoding.URL, headers: nil).responseJSON { (_, _, result) -> Void in
            
            if let value = result.value {
                let videoResults = value["Videos"] as? [Dictionary<String, AnyObject>]
                if videoResults != nil {
                    for item in videoResults! {
                        let video = Video(video: item)
                        self.videos.append(video)
                    }
                }
                NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
                    self.videoTableView.reloadData()
                    MBProgressHUD.hideHUDForView(self.view, animated: true)
                })
            }
            else {
                //MARK: something error
            }
        }
    }
    
}


extension LastestVideoController: UITableViewDataSource {
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return videos.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("videoCell", forIndexPath: indexPath) as! VideoCell
        cell.nameLabel.text = videos[indexPath.row].videoName
        cell.subjectLabel.text = "科属: " + videos[indexPath.row].subject.name
        cell.speakerLabel.text = "主讲人: " + videos[indexPath.row].speaker
        
        return cell
    }
}

extension LastestVideoController: UITableViewDelegate {
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 80
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
         //需要传的值
        let vedioController = self.storyboard!.instantiateViewControllerWithIdentifier("concreteMeetingController") as! MeetingDetailController
        vedioController.vedioURL = videos[indexPath.row].videoURL
        vedioController.vedioTitle = videos[indexPath.row].videoName
        vedioController.id = 2
        self.navigationController?.pushViewController(vedioController, animated: true)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
}
